<?php
 jimport('joomla.application.component.view');
 // Load the view framework
 if(!class_exists('VmView'))require(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'vmview.php');
 jimport('joomla.html.pane');
class svViewCanbo extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		switch ($task)
		{
			case 'add':
				$this->_add();
				break;
			case 'edit':
				$this->_edit();
				break;
			default :
				$this->_default();
				break;
		}
		parent::display();
	}
	// hien thi chi tiet
	function _default()
	{
		// ket noi voi model
		$model=$this->getModel('canbo');
		// kiem tra xem co phai la add hay edit form khong
		$action=JRequest::getVar('action');
		if($action=='') // vao default
		{
			//toolbar
			$this->toolbarDefault('Quản lý cán bộ ');
			// cac bien gia tri
			$page=JRequest::getInt('page');
			// xem co phai la xem chi tiet 1 can bo hay khong
			$idcanbo=JRequest::getVar('idcanbo');
			if($idcanbo==0)
			{
				jimport('joomla.html.pagination');
				// phan trang
				$total=$model->getTotal();
				$view=JRequest::getVar('view');
				global $mainframe;
				$param['limitstart']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.limitstart', 'limitstart',0,'int');
				$param['limit']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.limit', 'limit',$mainframe->getCfg('list_limit'),'int');
				$pager=new JPagination($total, $param['limitstart'], $param['limit']);
				// sap xep
				$param['order']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.order', 'filter_order','cb_id','cmd');
				$param['order_Dir']=$mainframe->getUserStateFromRequest('com_svquanlycanbo.'.$view.'.orderDir', 'filter_order_Dir','DESC','word');
				// get List
				$idcanbo=JRequest::getInt('sn_idcanbo');
				$donvi=JRequest::getInt('iddonvi');
				$phongban=JRequest::getInt('idphongban');
				$selected['canbo']=$idcanbo;
				$selected['donvi']=$donvi;
				$selected['phongban']=$phongban;
				$this->getList($selected);
				// fill du lieu
				$objectList=$model->getAllCanbo($param);
				$this->assignRef('total', $total);
				$this->assignRef('pager', $pager);
				$this->assignRef('param', $param);
				$this->assignRef('objectList', $objectList);
				//$this->_gridToolbar();
			}
			else 
			{
				$result=$model->getCanboByIDs($idcanbo);
			}
			$title='Quản lý các cán bộ';
			// set layout
			$this->assignRef('dataObject', $result);
			$this->assignRef('title', $title);
			$this->setLayout('default');
		}
		else
		{
			$ten=JRequest::getVar('ten');
			$this->assignRef('ten', $ten);
			$mieuta = JRequest::getVar('mieuta', '', 'POST','validation type',JREQUEST_ALLOWHTML);
			$this->assignRef('mieuta', $mieuta);
			$namsinh=JRequest::getVar('namsinh');
			$this->assignRef('namsinh', $namsinh);
			$quequan=JRequest::getVar('quequan');
			$this->assignRef('quequan', $quequan);
			$cmnd=JRequest::getVar('cmnd');
			$this->assignRef('cmnd', $cmnd);
			$gioitinh=JRequest::getVar('gioitinh');
			$this->assignRef('gioitinh', $gioitinh);
			$diachi=JRequest::getVar('diachi');
			$this->assignRef('diachi', $diachi);
			$dienthoai=JRequest::getVar('dienthoai');
			$this->assignRef('dienthoai', $dienthoai);
			$tongiao=JRequest::getVar('tongiao');
			$this->assignRef('tongiao', $tongiao);
			$honnhan=JRequest::getVar('honnhan');
			$this->assignRef('honnhan', $honnhan);
			$dantoc=JRequest::getVar('dantoc');
			$this->assignRef('dantoc', $dantoc);
			//-------- tbl quan ly chung
			$idphongban=JRequest::getInt('idphongban');
			$idchucvu=JRequest::getInt('idchucvu');
			$idcanbo=JRequest::getInt('idcanbo');
			$idchucdanh=JRequest::getInt('idchucdanh');
			// upload file anh
			$imgupload=$this->upload();
			if($imgupload=='') $imgupload='kaka.jpg';
			$this->assignRef('uploadimg', $imgupload);
			// select cac du lu di kem
			$listPhongban=$model->ListPhongban();
			$listChucvu=$model->getAllChucvu();
			$listChucdanh=$model->getAllChucdanh();
			$this->assignRef('listPhongban', $listPhongban);
			$this->assignRef('listChucvu', $listChucvu);
			$this->assignRef('listChucdanh', $listChucdanh);
			if($action=='add')// them moi mot can bo
			{
				//toolbar
				$this->toolbarAdd('Thêm mới cán bộ ');
				// set layout
				$this->setLayout('add');
			}
			else // edit mot can bo
			{
				$this->assignRef('idcanbo', $idcanbo);
				// lay du lieu tu model
				$canbo=$model->getCanboByID($idcanbo);
				// set layout
				$this->assignRef('canbo', $canbo);
				//toolbar
				$this->toolbarEdit('Chỉnh sửa cán bộ ');
				$this->setLayout('edit');
			}
		}

	}
	function _add()
	{
		header('location:index.php?option=com_svquanlycanbo&view=canbo&action=add');
	}
	function _edit()
	{
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idcanbo = $idlist[0];
		header('location:index.php?option=com_svquanlycanbo&view=canbo&action=edit&idcanbo='.$idcanbo);
	}
	// them cac button tren thanh toolbar
	function toolbarDefault($title)
	{
		$model=$this->getModel('canbo');
		$detail=$model->getDetailCanbo();
		JToolBarHelper::title($title);
		if($detail->ac_typeaccount!='canbo')
		{
			JToolBarHelper::addNew();
			JToolBarHelper::editListX();
			JToolBarHelper::deleteList();
		}
	}
	function toolbarAdd($title)
	{
		JRequest::setVar( 'hidemainmenu', 1 );
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::cancel();
	}
	function toolbarEdit($title)
	{
		JRequest::setVar( 'hidemainmenu', 1 );
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::apply();
		JToolBarHelper::cancel();
	}
	
	function getList($selected)
	{
		$model=$this->getModel('canbo');
		// list can bo
		$option[]=array('key'=>0,'text'=>JText::_('Chọn cán bộ'));
		$array=$model->Listcanbo();
		$array=array_merge($option,$array);
		$this->array=$array;
		$attr='class="inputbox", onchange="submitform()"';
		$list['canbo']=JHTML::_('select.genericlist',$array,'cb_id',$attr,'key','text',$selected['canbo']);
		// list phong ban
		$option1[]=array('key'=>0,'text'=>JText::_('Chọn phòng ban'));
		$array1=$model->ListPhongban();
		$array1=array_merge($option1,$array1);
		$this->array=$array1;
		$attr='class="inputbox", onchange="submitform()"';
		$list['phongban']=JHTML::_('select.genericlist',$array1,'idphongban',$attr,'key','text',$selected['phongban']);
		// list don vi
		$option2[]=array('key'=>0,'text'=>JText::_('Chọn đơn vị'));
		$array2=$model->ListDonvi();
		$array2=array_merge($option2,$array2);
		$this->array=$array2;
		$attr='class="inputbox", onchange="submitform()"';
		$list['donvi']=JHTML::_('select.genericlist',$array2,'iddonvi',$attr,'key','text',$selected['donvi']);
		$this->list=$list;
	}
	function upload()
	{
		jimport('joomla.client.helper');
		$allowedExts = array("jpg", "jpeg", "gif", "png");
		$extension = end(explode(".", $_FILES["file"]["name"]));
		if ((($_FILES["file"]["type"] == "image/gif")
				|| ($_FILES["file"]["type"] == "image/jpeg")
				|| ($_FILES["file"]["type"] == "image/png")
				|| ($_FILES["file"]["type"] == "image/pjpeg"))
				&& ($_FILES["file"]["size"] < 100000)
				&& in_array($extension, $allowedExts))
		{
			if ($_FILES["file"]["error"] > 0)
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
			}
			else
			{
				//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				//echo "Type: " . $_FILES["file"]["type"] . "<br />";
				//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
							$folder=JPATH_ADMINISTRATOR.DS."components".DS."com_svquanlycanbo".DS."uploads";
				if (file_exists($folder."/" . $_FILES["file"]["name"]))
				{
					echo '<b style="color:red;font-size:18px">Warning : '.$_FILES["file"]["name"] . " already exists. </b>";
					return $_FILES["file"]["name"];
				}
				else
				{
					move_uploaded_file($_FILES["file"]["tmp_name"],
					$folder."/" . $_FILES["file"]["name"]);
					echo '<b style="color:blue;font-size:18px">Upload image successful.! </b>';
					return $_FILES["file"]["name"];
				}
			}
		}
		else
		{
			//echo "Invalid file";
		}	
    }
}
?>